package com.nativetalk.service.three.coupon.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.coupon.TdNtCouponDetail;
import com.nativetalk.bean.live.TdNtLiveCourseHisDetails;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtCouponDao;
import com.nativetalk.dao.TdNtLiveCourseHisDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * Created by lizhun on 16/6/27.
 */
@Service
public class ThreeCouponInsertServiceImpl implements ThreeCouponInsertService {
    private static Logger logger = Logger.getLogger(ThreeCouponInsertServiceImpl.class);
    @Resource
    private TdNtCouponDao tdNtCouponDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Override
    public RetInfo addTdNtCoupon(TdNtCouponDetail tdNtCouponDetail, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":addTdNtCoupon:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            if (tdNtMember != null) {

                TdNtLiveCourseHisDetails tdNtLiveCourseHisDetails =
                        tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(tdNtCouponDetail.getLimit_id());
                if (tdNtLiveCourseHisDetails != null ) {
                    tdNtCouponDetail.setCoupon_amount(new BigDecimal("10"));
                    tdNtCouponDetail.setCoupon_limit_amount(new BigDecimal("0"));
                    tdNtCouponDetail.setMemb_id(tdNtMember.getMemb_id());

                    tdNtCouponDetail.setType(1);
                    tdNtCouponDetail.setLimit_time(new Timestamp(System.currentTimeMillis()+ 12 * 30 * 24 * 60 * 6 *1000l ));
                    TdNtCouponDetail detail = tdNtCouponDao.findTdNtCouponDetail(tdNtCouponDetail);
                    if (detail != null) {
                        retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                        retInfo.setTip("成功");
                    } else {
                        tdNtCouponDetail.setState(1);
                        tdNtCouponDao.insertTdNtCoupon(tdNtCouponDetail);
                        retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                        retInfo.setTip("成功");
                    }
                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("成功");
                }

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo addTdNtCouponFiveMin(int memb_id) {

        String logInfo = this.getClass().getName() + ":addTdNtCouponFiveMin:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            TdNtCouponDetail tdNtCouponDetail = new TdNtCouponDetail();
            tdNtCouponDetail.setCoupon_amount(new BigDecimal("5"));
            tdNtCouponDetail.setCoupon_limit_amount(new BigDecimal("0"));
            tdNtCouponDetail.setMemb_id(memb_id);
            tdNtCouponDetail.setLimit_id(0);
            tdNtCouponDetail.setType(2);
            tdNtCouponDetail.setTime(1);
            Long tim = 30*24*60*60*1000l;
            tdNtCouponDetail.setLimit_time(new Timestamp(System.currentTimeMillis() + tim));
            TdNtCouponDetail detail = tdNtCouponDao.findTdNtCouponDetail(tdNtCouponDetail);
            if (detail != null) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("已领取");
            } else {
                tdNtCouponDetail.setState(1);
                tdNtCouponDao.insertTdNtCoupon(tdNtCouponDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
